"use strict";

// worldScripts.WitchBank

this.name = "WitchBank";

this._pendingTransaction = null; // "deposit" or "withdrawal" - anything else is ignored

/* ====================================================================================
            SHOW BANKING SCREEN
======================================================================================= */

this._showBankingScreen = function (caller) {
    // If _pendingTransaction is set, process the caller as an amount
    if (this._pendingTransaction) {
        // Check if caller is a positive integer
        var amount = parseInt(caller, 10);
        if (!isNaN(amount) && amount > 0) {
            // Process the transaction
            this._performTransaction(this._pendingTransaction, amount);

            // Reset the pending transaction flag
            this._pendingTransaction = null;
        } else {
            player.consoleMessage("Invalid amount entered.", 6);
            this._pendingTransaction = null; // Reset on invalid input
        }
    }

    // Compose the default starting screen message
    var message = "Welcome to Witch Bank, the trusted name in interstellar banking. How can we help you today? Your current balances are: " +
                  missionVariables.WitchBank_cashAtBank + " credits in bank, " +
                  missionVariables.WitchBank_cashInWallet + " credits in wallet.";

    var optionsMenu = {
        "1_DEPOSIT": "Make a DEPOSIT",
        "2_WITHDRAWAL": "Make a WITHDRAWAL",
        "3_blank": "",
        "4_EXIT": "EXIT secure banking"
    };

    var parameters = {
        screenID: "BANKING_SCREEN",
        allowInterrupt: true,
        exitScreen: "GUI_SCREEN_INTERFACES",
        background: { name: "witchbank_background.png", height: 480 },
        title: "Witch Bank",
        message: message,
        choices: optionsMenu
    };

    // Adjust parameters for deposit/withdrawal screens
    switch (caller) {
        case "deposit":
            this._pendingTransaction = "deposit";
            parameters.title = "Wallet to Witch Bank";
            parameters.message = "Enter the amount you wish to deposit:";
            parameters.textEntry = true; // Replaces "choices" with a text entry field
            break;

        case "withdrawal":
            this._pendingTransaction = "withdrawal";
            parameters.title = "Witch Bank to Wallet";
            parameters.message = "Enter the amount you wish to withdraw:";
            parameters.textEntry = true; // Replaces "choices" with a text entry field
            break;

        default:
            // No changes to the starting screen
            break;
    }

    // Run the banking screen
    mission.runScreen(parameters, this._handleBankingChoice.bind(this));
};

/* ====================================================================================
            HANDLE BANKING CHOICE
======================================================================================= */

this._handleBankingChoice = function (choice) {
    if (choice === "4_EXIT") {
        return; // Exit the banking screen
    }

    if (choice === "1_DEPOSIT") {
        this._showBankingScreen("deposit");
    } else if (choice === "2_WITHDRAWAL") {
        this._showBankingScreen("withdrawal");
    } else {
        // Update text on F4 screen with new balance
        player.ship.dockedStation.setInterface("witch_bank", {
            title: "Witch Bank",
            category: expandDescription("[interfaces-category-organisations]"),
            summary: "Witch Bank is the trusted name in interstellar banking. Current balance: " + missionVariables.WitchBank_cashAtBank + " credits.",
            callback: this._showBankingScreen.bind(this)
        });
        // Redisplay the banking screen
        this._showBankingScreen("witch_bank");
    }
};

/* ====================================================================================
            PERFORM TRANSACTION
======================================================================================= */

this._performTransaction = function (transType, amount) {
    switch (transType) {
        case "deposit":
            // Transfer credits from wallet to bank
            if (amount > 0 && amount <= missionVariables.WitchBank_cashInWallet) {
                missionVariables.WitchBank_cashAtBank += amount;
                missionVariables.WitchBank_cashInWallet -= amount;
                player.consoleMessage("Deposited " + amount + " credits.", 6);
                log(this.name, "Deposited " + amount + " credits. New balances: Bank=" +
                    missionVariables.WitchBank_cashAtBank + ", Wallet=" +
                    missionVariables.WitchBank_cashInWallet);
            } else {
                player.consoleMessage("Invalid deposit amount.", 6);
            }
            break;

        case "withdrawal":
            // Transfer credits from bank to wallet
            if (amount > 0 && amount <= missionVariables.WitchBank_cashAtBank) {
                missionVariables.WitchBank_cashAtBank -= amount;
                missionVariables.WitchBank_cashInWallet += amount;
                player.consoleMessage("Withdrew " + amount + " credits.", 6);
                log(this.name, "Withdrew " + amount + " credits. New balances: Bank=" +
                    missionVariables.WitchBank_cashAtBank + ", Wallet=" +
                    missionVariables.WitchBank_cashInWallet);
            } else {
                player.consoleMessage("Invalid withdrawal amount.", 6);
            }
            break;

        default:
            player.consoleMessage("Invalid banking operation.", 6);
            break;
    }
};